<template>
<div class="login_container">
  <div class="login_box">
    <!-- logo -->
    <div class="logo"><img src="~assets/img/psb.jpg" alt="" /></div>
    <!-- 登陆表单 -->
    <el-form ref="loginFormRef" label-width="0" class="login_form" :model="loginForm" :rules="loginFormRules">
      <!-- 用户名表单项 -->
      <el-form-item prop="username">
        <el-input placeholder="用户名" prefix-icon="el-icon-user" v-model="loginForm.username" clearable></el-input>
      </el-form-item>
      <!-- 密码表单项 -->
      <el-form-item prop="password">
        <el-input type="password" placeholder="密码" prefix-icon="el-icon-lock" v-model="loginForm.password" clearable></el-input>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" plain @click="login">登陆</el-button>
        <el-button type="info" plain @click="resetLoginForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      // 
      loginForm: {
        username: "",
        password: "",
      },
      // 表单验证规则
      loginFormRules: {
        username: [{
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [{
            required: true,
            message: "请输入密码",
            trigger: "blur"
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 点击重置按钮重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 点击登录按钮表单预验证
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const {
          data: res
        } = await this.$request.post("login", this.loginForm);
        if (res.meta.status !== 200) {
          return this.$message.error("登陆失败");
        }
        this.$message({
          message: "登陆成功",
          type: "success",
        });
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #31c37c;

  .login_box {
    height: 300px;
    width: 450px;
    border-radius: 3px;
    background-color: #ffffff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .logo {
    // border: solid 1px red;
    border-radius: 50%;
    background-color: #ffffff;
    height: 130px;
    width: 130px;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);

    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translate(-50%);
  width: 85%;

  .el-input {
    background-color: rgb(235, 228, 228);
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
